 package cn.dnuihyy406.dnuifood_16110100406;


 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentPagerAdapter;
 import android.support.v4.view.ViewPager;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.ImageButton;
 import android.widget.ImageView;
 import android.widget.TextView;

 import java.util.ArrayList;
 import java.util.List;

 import cn.dnuihyy406.dnuifood_16110100406.controller.collectfragment.CollectFragment;
 import cn.dnuihyy406.dnuifood_16110100406.controller.homefragment.HomeFragment;
 import cn.dnuihyy406.dnuifood_16110100406.controller.mefragment.MeFragment;
 import cn.dnuihyy406.dnuifood_16110100406.controller.searchfragment.SearchFragment;

 public class MainActivity extends AppCompatActivity {
     private ImageButton nav_v1,nav_v2,nav_v3,nav_v4;
     private TextView T1,T2,T3,T4;
     private ViewPager vp;
     private List<Fragment> listfragment;
     private FragmentPagerAdapter fpadapter;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         nav_v1=findViewById(R.id.nav_v1);
         nav_v2=findViewById(R.id.nav_v2);
         nav_v3=findViewById(R.id.nav_v3);
         nav_v4=findViewById(R.id.nav_v4);
         T1=findViewById(R.id.nav_t1);
         T2=findViewById(R.id.nav_t2);
         T3=findViewById(R.id.nav_t3);
         T4=findViewById(R.id.nav_t4);
         vp=findViewById(R.id.viewpager1);
         initEvents();
         initFragment();
     }

     //    private void resetImage(){
//        V1.setImageResource(android.R.drawable.btn_star_big_off);
//        V2.setImageResource(android.R.drawable.btn_star_big_off);
//        V3.setImageResource(android.R.drawable.btn_star_big_off);
//        V4.setImageResource(android.R.drawable.btn_star_big_off);
//    }
//     private void selectTab(int i){
////        switch (i){
////            case 0:
////               // V1.setImageResource(android.R.drawable.btn_star_big_on);
////                break;
////            case 1:
////               // V2.setImageResource(android.R.drawable.btn_star_big_on);
////                break;
////            case 2:
////               // V3.setImageResource(android.R.drawable.btn_star_big_on);
////                break;
////            case 3:
////               // V4.setImageResource(android.R.drawable.btn_star_big_on);
////                break;
////        }
//         vp.setCurrentItem(i);
//
//     }
     private void resetImage() {
         nav_v1.setBackgroundResource(R.drawable.icon_home_sel);
         nav_v2.setBackgroundResource(R.drawable.icon_type_sel);
         nav_v3.setBackgroundResource(R.drawable.icon_search_sel);
         nav_v4.setBackgroundResource(R.drawable.icon_me_sel);
 }


     private void selectTab(int i){
         switch (i){
             case 0:
                 nav_v1.setBackgroundResource(R.drawable.icon_home_nor);
                 break;
             case 1:
                 nav_v2.setBackgroundResource(R.drawable.icon_type_nor);
                 break;
             case 2:
                 nav_v3.setBackgroundResource(R.drawable.icon_search_nor);
                 break;
             case 3:
                 nav_v4.setBackgroundResource(R.drawable.icon_me_nor);
                 break;
         }
         vp.setCurrentItem(i);
     }
     View.OnClickListener onClickListener = new View.OnClickListener() {
         @Override
         public void onClick(View view) {
              resetImage();
             switch (view.getId()){

                 case R.id.nav_v1:
                 case R.id.nav_t1:
                     selectTab(0);
                     break;

                 case R.id.nav_v2:
                 case R.id.nav_t2:
                     selectTab(1);
                     break;

                 case R.id.nav_v3:
                 case R.id.nav_t3:
                     selectTab(2);
                     break;

                 case R.id.nav_v4:
                 case R.id.nav_t4:
                     selectTab(3);
                     break;
             }
         }
     };
//点击底部控件切换试图
     private void initEvents(){
         nav_v1.setOnClickListener(onClickListener);
         nav_v2.setOnClickListener(onClickListener);
         nav_v3.setOnClickListener(onClickListener);
         nav_v4.setOnClickListener(onClickListener);
         T1.setOnClickListener(onClickListener);
         T2.setOnClickListener(onClickListener);
         T3.setOnClickListener(onClickListener);
         T4.setOnClickListener(onClickListener);
     }
     private void initFragment(){
         listfragment = new ArrayList<Fragment>();//数组
         listfragment.add(new HomeFragment());
         listfragment.add(new CollectFragment());
         listfragment.add(new SearchFragment());
         listfragment.add(new MeFragment());

         fpadapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
             @Override
             public Fragment getItem(int position) {
                 return  listfragment.get(position);
             }
//通过找id加载你想滑动的fragment，书（系统自带的函数）
             @Override
             public int getCount() {
                 return listfragment.size();
             }
         };
         vp.setAdapter(fpadapter);
         vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){

             @Override
             public void onPageScrolled(int i, float v, int i1)
             { }

             @Override
             public void onPageSelected(int i) {
                 vp.setCurrentItem(i);
                 //  resetImage();
                 selectTab(i);
             }

             @Override
             public void onPageScrollStateChanged(int i) {

             }
         });
     }
 }